home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Information / Mac Programming Secrets 1.0.1 / Chapter 10 / Hide Menubar.c < prev    next >
C/C++ Source or Header  |  1992-05-19  |  4KB  |  152 lines

  1. #include "Hide Menubar.h"
  2.  
  3. /*******************************************************************************
  4.  
  5.     Private constants and variables
  6.  
  7. *******************************************************************************/
  8.  
  9. const Boolean    kProhibitClicks    = FALSE;    /* Set to TRUE to prohibit the user
  10.                                                from clicking on the menubar
  11.                                                while hidden. If FALSE, the menu
  12.                                                will still respond to clicks. */
  13.  
  14. Boolean            gMenuBarHidden    = FALSE;    /* Current state of the menubar. */
  15.  
  16. short            gOldeMBarHeight;            /* Saves the height of the menubar
  17.                                                while we have it hidden. */
  18.  
  19. RgnHandle        gOldeGrayRgn;                /* Saves the region defining the
  20.                                                desktop; we change it when
  21.                                                hiding the menubar. */
  22.  
  23.  
  24. /*******************************************************************************
  25.  
  26.     ToggleMenuBar
  27.  
  28.     Routine that can be called by clients to change the state of the menubar.
  29.     It examines the current state of the menubar (as recorded by
  30.     gMenuBarHidden) and calls either ShowMenuBar or HideMenuBar as
  31.     appropriate.
  32.  
  33. *******************************************************************************/
  34. void ToggleMenuBar()
  35. {
  36.     if (gMenuBarHidden)
  37.         ShowMenuBar();
  38.     else
  39.         HideMenuBar();
  40. }
  41.  
  42.  
  43. /*******************************************************************************
  44.  
  45.     HideMenuBar
  46.  
  47.     Hides the menubar if it is visible. This is done by adding the space used
  48.     by the menubar to the desktop region. Normally, the menubar is clipped out
  49.     by the Window Manager, so nothing ever draws on top of it. We make it so
  50.     we can draw on top of it, and then call the Window Manager to refresh the
  51.     desktop.
  52.  
  53.     If we want to prohibit the user from clicking on the menubar while it’s
  54.     hidden, we set the height of the menubar to 0. Note the instruction we use
  55.     to accomplish this:
  56.  
  57.         GetMBarHeight() = 0;
  58.  
  59.     The menubar’s height is stored in a low-memory global at 0x0BAA. Normally
  60.     we use GetMBarHeight to retrieve this value to help determine things like
  61.     window placement. However, GetMBarHeight isn’t really a function; it’s a
  62.     macro:
  63.  
  64.         #define GetMBarHeight() (*(short*)0x0BAA)
  65.  
  66.     Because it’s a macro, we can use it on the left side of the equation. The
  67.     whole instruction expands to:
  68.  
  69.         (*(short*)0x0BAA) = 0;
  70.  
  71.     Which is just C’s way of saying “store zero into low-memory location
  72.     0x0BAA.”
  73.  
  74.     Note that if we should be careful about calling DrawMenuBar while the
  75.     menubar is hidden. While DrawMenuBar will faithfully draw the menubar, the
  76.     resulting image is very volatile. Any window dragged over the menubar will
  77.     wipe out chunks of it.
  78.  
  79. *******************************************************************************/
  80. void HideMenuBar()
  81. {
  82.     RgnHandle    menuRgn;
  83.  
  84.     if (!gMenuBarHidden) {
  85.         gOldeMBarHeight = GetMBarHeight();
  86.  
  87.         if (kProhibitClicks)
  88.             GetMBarHeight() = 0;
  89.  
  90.         if (gOldeGrayRgn == nil)
  91.             gOldeGrayRgn = NewRgn();
  92.  
  93.         CopyRgn(GetGrayRgn(), gOldeGrayRgn);
  94.  
  95.         menuRgn = NewRgn();
  96.         SetToMenuRect(menuRgn);
  97.         UnionRgn(GetGrayRgn(), menuRgn, GetGrayRgn());
  98.  
  99.         PaintBehind((WindowPeek) FrontWindow(), menuRgn);
  100.         CalcVisBehind((WindowPeek) FrontWindow(), menuRgn);
  101.  
  102.         DisposeRgn(menuRgn);
  103.  
  104.         gMenuBarHidden = TRUE;
  105.     }
  106. }
  107.  
  108.  
  109. /*******************************************************************************
  110.  
  111.     ShowMenuBar
  112.  
  113.     Restore the old desktop region, making the world safe for menubars again.
  114.     Call DrawMenuBar to redraw the menubar. If we previously set the menubar
  115.     height to zero in HideMenuBar, restore it.
  116.  
  117. *******************************************************************************/
  118. void ShowMenuBar()
  119. {
  120.     if (gMenuBarHidden) {
  121.         GetMBarHeight() = gOldeMBarHeight;
  122.         CopyRgn(gOldeGrayRgn, GetGrayRgn());
  123.         SetToMenuRect(gOldeGrayRgn);
  124.  
  125.         PaintBehind((WindowPeek) FrontWindow(), gOldeGrayRgn);
  126.         CalcVisBehind((WindowPeek) FrontWindow(), gOldeGrayRgn);
  127.  
  128.         DrawMenuBar();
  129.         gMenuBarHidden = FALSE;
  130.         SetEmptyRgn(gOldeGrayRgn);
  131.     }
  132. }
  133.  
  134.  
  135. /*******************************************************************************
  136.  
  137.     SetToMenuRect
  138.  
  139.     Handy utility for calculating and returning the bounding rectangle of the
  140.     menubar. Assumes that gOldeMBarHeight holds the correct height of the
  141.     menubar. The resulting rectangle is placed in the region passed in to us.
  142.  
  143. *******************************************************************************/
  144. void SetToMenuRect(RgnHandle rgn)
  145. {
  146.     Rect        menuRect;
  147.  
  148.     menuRect = qd.screenBits.bounds;
  149.     menuRect.bottom = gOldeMBarHeight;
  150.     RectRgn(rgn, &menuRect);
  151. }
  152.